package com.nl.controller.xuyi.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.nl.controller.common.model.BaseResult;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.controller.TimedTask;
import com.nl.controller.web.global.GlobalConstants;
import com.nl.controller.web.mapper.SmsMgrTemplateInfoMapper;
import com.nl.controller.web.service.SmsMgrTemplateInfoService;
import com.nl.controller.xuyi.bean.XuYiContentTemplateInfo;
import com.nl.controller.xuyi.bean.contentcheck.ContentCheckInfo;
import com.nl.controller.xuyi.bean.contentcheck.ContentCheckTypeBo;
import com.nl.controller.xuyi.mapper.XuYiCheckMapper;
import com.nl.controller.xuyi.mapper.XuYiContentTemplateMapper;
import com.nl.controller.xuyi.mapper.XuYiMgrTemplateMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * @desc
 * @return null
 * @author ll
 * @date 2020/10/12 14:42
 */
@Service
@Slf4j
public class XuYiContentCheckService {
	@Autowired
	XuYiCheckMapper xuYiContentCheckMapper;

	@Autowired
	SmsMgrTemplateInfoMapper smsMgrTemplateInfoMapper;

	@Autowired
	XuYiContentTemplateMapper xuYiContentTemplateMapper;

	@Autowired
	XuYiMgrTemplateMapper xuYiMgrTemplateMapper;

	@Autowired
	private SmsMgrTemplateInfoService smsMgrTemplateInfoService;

	@Autowired
	private CommonService commonService;

	public BaseResult<List<ContentCheckTypeBo>> findCheckType(String checkType){
		BaseResult<List<ContentCheckTypeBo>> result = BaseResult.newFailInstance();
		List<ContentCheckTypeBo> contentCheckTypeList ;
		try {
			contentCheckTypeList =xuYiContentCheckMapper.findCheckType(checkType);
			result.setData(contentCheckTypeList);
			result.setRetCode(GlobalConstants.SUCCESS_CODE);
		}catch (Exception e){
			result.setRetMsg("查询短信审核类型列表发生异常："+e.toString());
			e.printStackTrace();
		}
		return result ;
	}

    public PageInfo<ContentCheckInfo> findCheckList(int page,int limit,String checkType, String checkState) {
		PageHelper.startPage(page, limit);
		ContentCheckInfo contentCheckInfo = new ContentCheckInfo();
		List<ContentCheckInfo> contentCheckInfoList;
		PageInfo<ContentCheckInfo> pageInfo = new PageInfo<>();
		try {
			contentCheckInfo.setCheckType(checkType);
			contentCheckInfo.setCheckState(checkState);

			contentCheckInfoList = xuYiContentCheckMapper.findCheckList(contentCheckInfo);
			pageInfo = new PageInfo<>(contentCheckInfoList);
		}catch (Exception e){
			log.info("查询短信审核列表信息发生异常："+e.toString());
			e.printStackTrace();
		}
		return  pageInfo;
    }

	public BaseResult<ContentCheckInfo> findCheckDetail(ContentCheckInfo contentCheckInfo) {
		BaseResult<ContentCheckInfo> result = BaseResult.newFailInstance();
		List<ContentCheckInfo> contentCheckInfoList;
		ContentCheckInfo checkInfoDetail;
		String mgrId;
		String mgrName;
		String contentTempId;
		String contentTempName;
		try {
			contentCheckInfoList = xuYiContentCheckMapper.findCheckList(contentCheckInfo);
			if(!CollectionUtils.isEmpty(contentCheckInfoList)){
				checkInfoDetail = contentCheckInfoList.get(0);
				mgrId = checkInfoDetail.getMgrId();
				contentTempId = checkInfoDetail.getContentTempId();

				/*if(StringUtil.isNotEmpty(contentTempId)){
					contentTempName =xuYiContentTemplateMapper.findContentTemplateByContentId(contentTempId).getContentName();
					checkInfoDetail.setContentTempName(contentTempName);
				}*/

				if(StringUtil.isNotEmpty(mgrId)){
					mgrName =smsMgrTemplateInfoMapper.findMgrTemplateByMgrId(mgrId).getName();
					checkInfoDetail.setMgrName(mgrName);
				}
				result.setData(checkInfoDetail);
			}else{
				result.setRetMsg("查询短信审核详细信息结果为空");
			}
			result.setRetCode(GlobalConstants.SUCCESS_CODE);
		}catch (Exception e){
			result.setRetMsg("查询短信审核详细信息发生异常："+e.toString());
			e.printStackTrace();
		}
		return result;
	}

	public BaseResult updateCheckInfo(ContentCheckInfo contentCheckInfo) {
		BaseResult result = BaseResult.newFailInstance();
		int updateResult = 0;
		try {
			//增加审核次数
			String checkState = contentCheckInfo.getCheckState();
			contentCheckInfo.setCheckState(null);
			ContentCheckInfo contentCheckInfo1 = xuYiContentCheckMapper.findCheckList(contentCheckInfo).get(0);
			contentCheckInfo1.setRemark(contentCheckInfo.getRemark());
			contentCheckInfo1.setCheckState(checkState);
			contentCheckInfo1.setCheckNumber(Integer.parseInt(contentCheckInfo1.getCheckNumber())+1+"");
			contentCheckInfo1.setCheckOperatorTel(contentCheckInfo.getCheckOperatorTel());
			updateResult = xuYiContentCheckMapper.updateCheckInfo(contentCheckInfo1);
			String checkType = contentCheckInfo1.getCheckType();
			String checkMethod = contentCheckInfo.getCheckMethod();
			//插入短信上行审核模块内存，以防重复审核。
			//短信模板审核
			if("1".equals(checkType)||"2".equals(checkType)||"3".equals(checkType)){
				TimedTask.map.put(contentCheckInfo1.getContentTempId()+"+"+contentCheckInfo1.getCheckNumber()+"DSXYMB",contentCheckInfo.getCheckOperatorTel());
				TimedTask.map.put(contentCheckInfo1.getContentTempId()+"+"+contentCheckInfo1.getCheckNumber()+"LDXYMB",contentCheckInfo.getCheckOperatorTel());
			}else if("4".equals(checkType)||"5".equals(checkType)||"6".equals(checkType)){
				//短信内容审核
				TimedTask.map.put(contentCheckInfo1.getMgrId()+"+"+contentCheckInfo1.getCheckNumber()+"LDXM",contentCheckInfo.getCheckOperatorTel());
				TimedTask.map.put(contentCheckInfo1.getMgrId()+"+"+contentCheckInfo1.getCheckNumber()+"DSXM",contentCheckInfo.getCheckOperatorTel());
				//更新项目模板审核次数
				Integer result2 = smsMgrTemplateInfoService.updateCheckNum(contentCheckInfo1.getCheckNumber()+"",contentCheckInfo1.getMgrId());
			}

			String type = "";
			String remark= "";
			String operateResult ="";
			String menu = "";
			String operatorTel = contentCheckInfo.getCheckOperatorTel();

			//通过短信上行审核
			if("2".equals(checkMethod)){
				type = "8";
				menu = "短信模板管理";
				//审核通过
				if("1".equals(checkState)){
					operateResult = "0";
					if("1".equals(checkType)){
						remark = "短信审核新增短信模板【id:"+contentCheckInfo1.getContentTempId() +"】-通过";
					}else if("2".equals(checkType)){
						remark = "短信审核修改短信模板【id:"+contentCheckInfo1.getContentTempId() +"】-通过";
					}else if("3".equals(checkType)){
						remark = "短信审核删除短信模板【id:"+contentCheckInfo1.getContentTempId() +"】-通过";
					}else if("4".equals(checkType)){
						remark = "短信审核新增短信内容【id:"+contentCheckInfo1.getMgrId() +"】-通过";
					}else if("5".equals(checkType)){
						remark = "短信审核修改短信内容【id:"+contentCheckInfo1.getMgrId() +"】-通过";
					}else if("6".equals(checkType)){
						remark = "短信审核删除短信内容【id:"+contentCheckInfo1.getMgrId() +"】-通过";
					}
					//审核不通过
				}else if("2".equals(checkState)){
					operateResult = "1";
					if("1".equals(checkType)){
						remark = "短信审核新增短信模板【id:"+contentCheckInfo1.getContentTempId() +"】-不通过";
					}else if("2".equals(checkType)){
						remark = "短信审核修改短信模板【id:"+contentCheckInfo1.getContentTempId() +"】-不通过";
					}else if("3".equals(checkType)){
						remark = "短信审核删除短信模板【id:"+contentCheckInfo1.getContentTempId() +"】-不通过";
					}else if("4".equals(checkType)){
						remark = "短信审核新增短信内容【id:"+contentCheckInfo1.getMgrId() +"】-不通过";
					}else if("5".equals(checkType)){
						remark = "短信审核修改短信内容【id:"+contentCheckInfo1.getMgrId() +"】-不通过";
					}else if("6".equals(checkType)){
						remark = "短信审核删除短信内容【id:"+contentCheckInfo1.getMgrId() +"】-不通过";
					}
				}
				commonService.createSmsOperLogUpward(type,remark,operateResult,menu,operatorTel);
			}else{
				type = "8";
				menu = "短信模板管理";
				//审核通过
				if("1".equals(checkState)){
					operateResult = "0";
					if("1".equals(checkType)){
						remark = "页面审核新增短信模板【id:"+contentCheckInfo1.getContentTempId() +"】-通过";
					}else if("2".equals(checkType)){
						remark = "页面审核修改短信模板【id:"+contentCheckInfo1.getContentTempId() +"】-通过";
					}else if("3".equals(checkType)){
						remark = "页面审核删除短信模板【id:"+contentCheckInfo1.getContentTempId() +"】-通过";
					}else if("4".equals(checkType)){
						remark = "页面审核新增短信内容【id:"+contentCheckInfo1.getMgrId() +"】-通过";
					}else if("5".equals(checkType)){
						remark = "页面审核修改短信内容【id:"+contentCheckInfo1.getMgrId() +"】-通过";
					}else if("6".equals(checkType)){
						remark = "页面审核删除短信内容【id:"+contentCheckInfo1.getMgrId() +"】-通过";
					}
					//审核不通过
				}else if("2".equals(checkState)){
					operateResult = "1";
					if("1".equals(checkType)){
						remark = "页面审核新增短信模板【id:"+contentCheckInfo1.getContentTempId() +"】-不通过";
					}else if("2".equals(checkType)){
						remark = "页面审核修改短信模板【id:"+contentCheckInfo1.getContentTempId() +"】-不通过";
					}else if("3".equals(checkType)){
						remark = "页面审核删除短信模板【id:"+contentCheckInfo1.getContentTempId() +"】-不通过";
					}else if("4".equals(checkType)){
						remark = "页面审核新增短信内容【id:"+contentCheckInfo1.getMgrId() +"】-不通过";
					}else if("5".equals(checkType)){
						remark = "页面审核修改短信内容【id:"+contentCheckInfo1.getMgrId() +"】-不通过";
					}else if("6".equals(checkType)){
						remark = "页面审核删除短信内容【id:"+contentCheckInfo1.getMgrId() +"】-不通过";
					}
				}
				commonService.createSmsOperLog(type,remark,operateResult,menu);
			}


			result.setRetCode(GlobalConstants.SUCCESS_CODE);
			result.setData(updateResult);
			result.setRetMsg("更新短信内容审核表状态成功");
			log.info("更新短信内容审核表状态成功");
		}catch (Exception e){
			result.setRetMsg("更新短信内容审核表状态发生异常"+e.toString());
			e.printStackTrace();
		}
		return result;
	}

	public BaseResult dealCheckResult(String checkId) {
		BaseResult baseResult = BaseResult.newFailInstance();
		int result = 0;
		ContentCheckInfo contentCheckInfoReq = new ContentCheckInfo();
		List<ContentCheckInfo> contentCheckInfoList;
		ContentCheckInfo contentCheckInfo ;
		String checkType ;
		try {
			contentCheckInfoReq.setCheckId(checkId);
			contentCheckInfoList = xuYiContentCheckMapper.findCheckList(contentCheckInfoReq);
			if(!CollectionUtils.isEmpty(contentCheckInfoList)){
				contentCheckInfo = contentCheckInfoList.get(0);
				checkType = contentCheckInfo.getCheckType();
				result = this.checkAddContentTemp(contentCheckInfo,checkType);
				if(result>0){
					baseResult.setRetMsg("审核通过后变更短信模板表成功");
					baseResult.setRetCode(GlobalConstants.SUCCESS_CODE);
					baseResult.setData(result);
				}else{
					baseResult.setRetMsg("审核通过后变更短信模板表失败");
					baseResult.setData(result);
				}
			}
		}catch (Exception e){
			log.info("审核通过后变更短信模板表发生异常"+e.toString());
			e.printStackTrace();
		}
		return baseResult;
	}
	
	/**
	 * @desc 根据审核类型变更短信模板表
	 * @param contentCheckInfo 
 * @param checkType 
	 * @return int
	 * @author ll
	 * @date 2020/10/13 17:13
	 */
	private int checkAddContentTemp(ContentCheckInfo contentCheckInfo,String checkType){
		int updateResult = 0;
		XuYiContentTemplateInfo xuYiContentTemplateInfo = new XuYiContentTemplateInfo();
		try {
			xuYiContentTemplateInfo.setContentId(contentCheckInfo.getContentTempId());
			xuYiContentTemplateInfo.setContentName(contentCheckInfo.getContentTempName());
			xuYiContentTemplateInfo.setContentContent(contentCheckInfo.getContentTemp());
			xuYiContentTemplateInfo.setUserMsisdn(contentCheckInfo.getOperatorTel());
			xuYiContentTemplateInfo.setContentState("0");
			if("1".equals(checkType)){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String createTime = sdf.format(new Date());
				xuYiContentTemplateInfo.setCreateTime(createTime);
				//审核通过
				xuYiContentTemplateInfo.setAuditState("1");
				//审核次数
				log.info("----"+contentCheckInfo.getCheckNumber());
				xuYiContentTemplateInfo.setCheckNumber(contentCheckInfo.getCheckNumber());
				xuYiContentTemplateInfo.setTempChecker(contentCheckInfo.getCheckOperatorTel());
				updateResult = xuYiContentTemplateMapper.insertContentTemplate(xuYiContentTemplateInfo);
			}else if("2".equals(checkType)){
				String checkNunber = xuYiContentTemplateMapper.findContentTemplateByContentId(contentCheckInfo.getContentTempId()).getCheckNumber();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String modifyTime = sdf.format(new Date());
				xuYiContentTemplateInfo.setModifyTime(modifyTime);
				xuYiContentTemplateInfo.setCheckNumber(Integer.valueOf(checkNunber)+1+"");
				updateResult = xuYiContentTemplateMapper.modifyContentTemplate(xuYiContentTemplateInfo);
			}else if("3".equals(checkType)){
				updateResult = xuYiContentTemplateMapper.deleteContentTemplate(contentCheckInfo.getContentTempId());
			}else if("4".equals(checkType)){
				updateResult = 1;
			}else if ("5".equals(checkType)){
				System.out.println(checkType.toString());
				updateResult =xuYiMgrTemplateMapper.modifyContent(contentCheckInfo.getMgrId(),contentCheckInfo.getMgrContent(),contentCheckInfo.getMgrContentType());
			}
		}catch (Exception e){
			log.info("审核通过后变更短信模板表发生异常:"+e.toString());
			e.printStackTrace();
		}
		return updateResult;
	}


}
